//
//  CollectCell.swift
//  AliBaiChuan
//
//  Created by 郑东喜 on 2017/2/22.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit

protocol CollectCellDelegate {
    /// 单机的cell的位置
    ///
    /// - Parameters:
    ///   - str: <#str description#>
    ///   - clickIndex: <#clickIndex description#>
    func clickAtItemIndex(str : String,clickIndex : NSIndexPath)
}

class CollectCell: UICollectionViewCell {
    
    let space : CGFloat = 5
    
    lazy var button: UIButton = {
        let b : UIButton = UIButton.init(frame: CGRect.init(x: self.space, y: self.space, width: 45 , height: 45))
        b.backgroundColor = UIColor.randomColor()
        b.layer.cornerRadius = b.Width * 0.5
        return b
    }()
    
    
    ///商品图片
    lazy var imgView: UIImageView = {
        let img : UIImageView = UIImageView.init(frame: CGRect.init(x: self.space, y: self.space, width: self.frame.width - 2 * 5, height: self.frame.width))
        img.backgroundColor = UIColor.clear
        
        img.contentMode = .scaleAspectFit
        img.layer.borderWidth = 1
        return img
    }()
    
    ///商品名称
    lazy var shopName: UILabel = {
        let tt : UILabel = UILabel.init(frame: CGRect.init(x: 5, y: 2 * 5 + self.frame.width, width: self.frame.width - 10, height: 40))
        
        tt.text = "啊说来就大姐大色季拉山级大神解答时；就打算的阿来得及拉丝机大山里的骄傲圣诞节圣诞节三就啊说来就大姐大色季拉山级大神解答时；"
        
        tt.numberOfLines = 0
        tt.lineBreakMode = .byWordWrapping
        tt.layer.borderWidth = 1
        
        tt.font = UIFont.boldSystemFont(ofSize: 12)
        
        ///获取实时文本的大小
        let addSize : CGSize = tt.sizeThatFits(CGSize(width: tt.frame.size.width, height: CGFloat(MAXFLOAT)))
        
        tt.frame = CGRect(x: tt.frame.origin.x, y: tt.frame.origin.y, width: tt.frame.size.width, height: addSize.height)
        
//        tt.sizeToFit()
        return tt
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.lightGray
        
        button.center = self.contentView.center
        self.prepareU()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension CollectCell {
    fileprivate func prepareU() -> Void {

        ///图片
        contentView.addSubview(imgView)
        
        ///商品名字
        contentView.addSubview(shopName)
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.shopName.sizeToFit()
    }
}

